cmake_minimum_required(VERSION 3.10.0)

project(Tiny-Serial-Terminal LANGUAGES CXX VERSION 3.0.0)
configure_file(${PROJECT_SOURCE_DIR}/Code/config.h.in config.h)

# 通过此选项配置目标平台
set(TARGET_PLATFORM "Windows")
# set(TARGET_PLATFORM "Linux")

message(STATUS "operation system is ${CMAKE_SYSTEM}")
message(STATUS ${CMAKE_GENERATOR})

# 本配置文件不支持MSVC
# if (CMAKE_SYSTEM_NAME MATCHES "Windows")
#     message(FATAL_ERROR "Windows is not supported for this CMakeLists.txt")
# endif (CMAKE_SYSTEM_NAME MATCHES "Windows")

SET(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++17")

if(TARGET_PLATFORM STREQUAL "Linux")
    # 设置toolchain文件路径
    set(CMAKE_TOOLCHAIN_FILE
        ${CMAKE_SOURCE_DIR}/cmake/LinuxConfig.cmake)
elseif(TARGET_PLATFORM STREQUAL "Windows")
    # add_definitions(-D__MINGW64__)
    # add_definitions(-D_WIN64)
    # add_definitions(-DWIN64)
    # add_definitions(-DUNICODE)
    add_definitions(-D_POSIX_THREAD_SAFE_FUNCTIONS)
    set(CMAKE_TOOLCHAIN_FILE
            ${CMAKE_SOURCE_DIR}/cmake/WindowsConfig.cmake)
else()
    message(FATAL_ERROR "\r\nTARGET_PLATFORM=${TARGET_PLATFORM}\r\nTARGET_PLATFORM must be \"Linux\" or \"Windows\"")
endif(TARGET_PLATFORM STREQUAL "Linux")
message(STATUS "Current target platform: "${TARGET_PLATFORM})

include(${CMAKE_TOOLCHAIN_FILE})

message(STATUS "Project version: " ${PROJECT_VERSION})


message(STATUS ${CMAKE_CXX_FLAGS})

set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build/bin)


message(STATUS "C_COMPILER: " ${CMAKE_C_COMPILER})
message(STATUS "CXX_COMPILER: " ${CMAKE_CXX_COMPILER})
if (NOT ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU"))
    message(FATAL_ERROR "Compiler must be GNU")
endif()


aux_source_directory(${PROJECT_SOURCE_DIR}/Code SRC_LIST)
aux_source_directory(${PROJECT_SOURCE_DIR}/Code/XYModem XYM_LIST)
aux_source_directory(${PROJECT_SOURCE_DIR}/Code/SerialControl SC_LIST)

add_executable(
    ${PROJECT_NAME}
    ${SRC_LIST}
    ${XYM_LIST}
    ${SC_LIST}
    )

# add_definitions(-D_XOPEN_SOURCE)
# add_definitions(-D_GNU_SOURCE)
add_definitions(-D_POSIX_THREAD_SAFE_FUNCTIONS)
# aux_source_directory(${PROJECT_SOURCE_DIR}/Test TEST_LIST)
add_executable(
    test
        Test/toml_command.cpp)



if(TARGET_PLATFORM STREQUAL "Windows")
    message(STATUS "link library \"wsock32\" and \"setupapi\"")
    target_link_libraries(${PROJECT_NAME} PUBLIC wsock32)
    target_link_libraries(${PROJECT_NAME} PUBLIC setupapi)
endif()



set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(${PROJECT_NAME} PUBLIC Threads::Threads)
target_link_libraries(test PUBLIC Threads::Threads)


if ((CMAKE_SYSTEM_NAME MATCHES "Windows") OR (CMAKE_SYSTEM_NAME MATCHES "WIN32"))
    set(BOOST_ROOT "D:/Documents/00_Workspace/boost_1_79_0")
    set(Boost_INCLUDE_DIR "D:/Documents/00_Workspace/boost_1_79_0/include/boost-1_79/")
    set(BOOST_LIBRARYDIR "D:/Documents/00_Workspace/boost_1_79_0/lib")
    set(Boost_NO_SYSTEM_PATHS ON)
endif()
message(STATUS ${BOOST_ROOT})

find_package(Boost COMPONENTS REQUIRED)
if(Boost_FOUND)
    include_directories(${Boost_INCLUDE_DIRS})
    message( STATUS "Boost_INCLUDE_DIRS = ${Boost_INCLUDE_DIRS}.")
    message( STATUS "Boost_LIBRARIES    = ${Boost_LIBRARIES}.")
    message( STATUS "Boost_LIB_VERSION  = ${Boost_LIB_VERSION}.")

    target_link_libraries(${PROJECT_NAME} PUBLIC Boost::boost)
    target_link_libraries(test PUBLIC Boost::boost)
    add_definitions(-DBOOST_BIND_GLOBAL_PLACEHOLDERS)
endif()
